package com.tgy.programmer_code_interview_guide._01_stack_queue;

import lombok.Data;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: tgy @Date: 1/18/21 2:23 PM
 *
 * 实现一种狗猫队列的结构，要求如下:
 * 1.用户可以调用add方法将cat类或dog类的实例放入队列中;
 *
 * 2.用户可以调用pollAll方法，将队列中所有的实例按照进队列的先后顺序依次弹出;
 *
 * 3.用户可以调用pollDog方法，将队列中dog类的实例按照进队列的先后顺序依次弹出;
 *
 * 4.用户可以调用pollCat方法,将队列中cat类的实例按照进队列的先后顺序依次弹出;
 *
 * 5.用户可以调用isEmpty方法，检查队列中是否还有dog或cat的实例;
 *
 * 6.户可以调用isDogEmpty 方法，检查队列中是否有dog类的实例;
 *
 * 7.用户可以调用isCatEmpty方法，检查队列中是否有cat类的实例。
 *
 * 要点:
 * 1)使用一个count记录进入队列次序，
 *
 * 2)使用两个队列来分别存储
 */

class Pet{

    private String type;

    public Pet(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

class Cat extends Pet{

    public Cat() {
        super("cat");
    }

    @Override
    public String toString() {
        return "Cat{}";
    }
}

class Dog extends Pet{


    public Dog() {
        super("dog");
    }

    @Override
    public String toString() {
        return "Dog{}";
    }
}

public class _04_猫狗队列 {

    private static class PetCount{

        private Pet pet;
        private int count;

        public PetCount(Pet pet, int count) {
            this.pet = pet;
            this.count = count;
        }

        public Pet getPet() {
            return pet;
        }

        public int getCount() {
            return count;
        }
    }

    private Queue<PetCount> dogQueue = new LinkedList<>();
    private Queue<PetCount> catQueue = new LinkedList<>();

    private int count;

    public void add(Pet pet){

        if (pet instanceof Dog) {

            dogQueue.offer(new PetCount(pet,count++));
        }else if (pet instanceof Cat) {

            catQueue.offer(new PetCount(pet,count++));
        }else {

            throw new IllegalArgumentException("pet 必须是dog或者cat");
        }
    }


    public Pet pollAll() {

        if (dogQueue.isEmpty() && catQueue.isEmpty()) {

            throw new RuntimeException("队列为空");
        }

        if (dogQueue.isEmpty()) {

            return catQueue.poll().pet;
        }else if (catQueue.isEmpty()){

            return dogQueue.poll().pet;
        }

        PetCount dogCount = dogQueue.peek();
        PetCount catCount = catQueue.peek();

        if (dogCount.count < catCount.count) {

            return dogQueue.poll().pet;
        }else {

            return catQueue.poll().pet;
        }

    }

    public Pet pollDog(){

        if (dogQueue.isEmpty()) {

            throw new RuntimeException("队列为空");
        }

        return dogQueue.poll().pet;
    }

    public Pet pollCat() {

        if (catQueue.isEmpty()) {

            throw new RuntimeException("队列为空");
        }

        return catQueue.poll().pet;
    }

    public boolean isEmpty() {

        return catQueue.isEmpty() && dogQueue.isEmpty();
    }

    public boolean isDogEmpty(){

        return dogQueue.isEmpty();
    }

    public boolean isCatEmpty() {

        return catQueue.isEmpty();
    }

    public static void main(String[] args) {

        _04_猫狗队列 queue = new _04_猫狗队列();

        queue.add(new Dog());
        queue.add(new Cat());
        queue.add(new Dog());
        queue.add(new Cat());
        queue.add(new Dog());
        queue.add(new Dog());


        while (!queue.isDogEmpty()) {

            System.out.println(queue.pollDog());
        }

    }
}
